package com.kbbywyclient.widget.ptr;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by Jiangzx on 2017/8/16 17:07.
 */

public class PullRefreshLayout extends LinearLayout implements PtrUIHandler {
    private static final String PULL_TO_REFRESH = "下拉刷新";
    private static final String RELEASE_TO_REFRESH = "松开开始刷新";
    private static final String REFRESH_ING = "正在刷新...";
    private static final String REFRESH_COMPLETE = "刷新完成";
    private float mTextSize=14;//默认字体大小 14sp
    private int mTextColor=0xFF4BD2CC;//默认颜色
    TextView textView;
    public PullRefreshLayout(Context context) {
        this(context,null);
    }

    public PullRefreshLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PullRefreshLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textView=new TextView(context);
        textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        textView.setTextColor(mTextColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
        addView(textView);
        setGravity(Gravity.CENTER);
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        textView.setText(PULL_TO_REFRESH);
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        textView.setText(REFRESH_ING);
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        textView.setText(REFRESH_COMPLETE);
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        final int mOffsetToRefresh = frame.getOffsetToRefresh();
        final int currentPos = ptrIndicator.getCurrentPosY();
        final int lastPos = ptrIndicator.getLastPosY();
        if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                textView.setText(PULL_TO_REFRESH);
            }
        }else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
            if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
                if(!frame.isPullToRefresh()){
                    textView.setText(RELEASE_TO_REFRESH);
                }
            }
        }

    }

    public void setTextColor(@ColorInt int textColor){
        this.mTextColor=textColor;
        this.textView.setTextColor(mTextColor);
    }

    public void setTextSize(float textSize){
        this.mTextSize=textSize;
        this.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
    }

}
